/*
 * Class that handle the persistance of book class
 * in a SQL database
 * @author G. Piraux, M-A. Taupenot
 * @version 1.0.0
 */
package booksManagement;

import java.util.List;
import exceptions.*;
import booksManagement.Book;
import jpaUtils.JPAUtil;
import javax.persistence.EntityManager;

/** 
 * create the entityManager associated to the book library
 * @author user
 */
public class BookDAO {

  /**
   * Function that tests if the Book table is empty or not and return true or false
	 * called when you try to create the first book in the PersistentLibraryTest
   * @return
   */
	public boolean isEmpty(){
		EntityManager entityManager=JPAUtil.getEntityManager();
		if (entityManager.createNamedQuery("findAllBooks").getResultList().isEmpty())
			return true;
		else
			return false;	
	}





  /**
   * Function that return the table of books
   * @return
   */
	@SuppressWarnings("unchecked")
  public List<Book> getContent(){
		EntityManager entityManager=JPAUtil.getEntityManager();
		return ((List<Book>)entityManager.createNamedQuery("findAllBooks").getResultList());
	}





  /**
   * Function that try to add a book to the table and throw an exception if fails
   * @param book
   * @return
   */
	public boolean add(Book book){
			EntityManager entityManager=JPAUtil.getEntityManager();
	try{
				entityManager.persist(book);
			} catch (Exception e) {
				System.err.println("Problem when saving");
				e.printStackTrace();
				return false;
			}
			return true;
		}





	/**
	 * Function that print the informations of every book in the table
	 * @return
	 */
	@Override
	public String toString(){
		EntityManager entityManager=JPAUtil.getEntityManager();
		String result="";
		@SuppressWarnings("unchecked")
     List<Book> theBooks = (List<Book>) entityManager.createNamedQuery("findAllBooks").getResultList();
		for (Book b : theBooks)
			result += b.toString()+ "\n";
				return result;
		}




	/**
	 * Function that looks for a certain book in the table regarding its Id
	 * @param bookId
	 * @return
	 * @throws BookExistsException
	 */
	public Book get(long bookId) throws BookExistsException {
		EntityManager entityManager=JPAUtil.getEntityManager();
		Book b = (Book) entityManager.find(Book.class, bookId);
		if (b ==null) throw new BookExistsException();
		else return b;
		}





	/**
	 * Funtion that returns the number of books contained in the table
	 * @return
	 */
	public long size(){
		EntityManager entityManager=JPAUtil.getEntityManager();
		return (entityManager.createNamedQuery("findAllBooks").getResultList().size());
		}




	/**
	 * Funtion that try to delete a book in the table and throw an exception if fails
	 * @param book
	 * @return
	 * @throws BookExistsException
	 */
	public boolean remove(Book book) throws BookExistsException {
		EntityManager entityManager=JPAUtil.getEntityManager();
		if (book== null) 
			throw new BookExistsException(); 
		else 
			try{
				entityManager.remove(book);
			} catch (Exception pe) {
				System.err.println("problem when deleting an entity ");
				pe.printStackTrace();
				return false;
			}
		return true;	
		}





	/**
	 * Funtion that test if the book gived in parameter is registered in the table and throws an exception if fails
	 * @param book
	 * @return
	 * @throws BookExistsException
	 */
	public boolean contains(Book book) throws BookExistsException {
		EntityManager entityManager=JPAUtil.getEntityManager();
		if (book == null) throw new BookExistsException();
		Book b = (Book) entityManager.find(Book.class, book.getIsbn_isan());
		if (b ==null) return false;
		else return b.equals(book);	
		}

}


